package com.qf.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCouponUserMapper;
import com.qf.pojo.DtsCouponUser;
import com.qf.service.DtsCouponUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Ypl
 * @version V1.0
 * @Project September-mavens
 * @Package com.qf.serviceimpl
 * @Description:
 * @Date 2022/10/21 20:45
 */
@SuppressWarnings("all")
@Service
public class DtsCouponUserServiceImpl implements DtsCouponUserService {
    @Autowired
    DtsCouponUserMapper dtsCouponUserMapper;


    @Override
    public Map<String, Object> findByPageAndCouponId(Integer page, Integer limit, String order, String sort, Integer couponId, Integer userId, Integer status) {
        IPage<DtsCouponUser> iPage = new Page<>(page,limit);

        QueryWrapper<DtsCouponUser> wrapper = new QueryWrapper<>();
        if(userId!=null){
            wrapper.eq("user_id",userId);
        }
        if(status!=null){
            wrapper.eq("status",status);
        }
        if("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc(sort);
        }else{
            wrapper.orderByAsc(sort);
        }
        wrapper.eq("coupon_id",couponId);
        wrapper.eq("deleted",false);
        dtsCouponUserMapper.selectPage(iPage,wrapper);

        Map<String,Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());

        List<DtsCouponUser> list = new ArrayList<>();
        //将查询到的数据进行遍历
        List<DtsCouponUser> records = iPage.getRecords();
        for (DtsCouponUser record : records) {
            list.add(record);
        }
        map.put("items",list);
        return map;
    }
}
